package com.rcloud.server.sealtalk.service;

import com.rcloud.server.sealtalk.dao.GroupsMapper;
import com.rcloud.server.sealtalk.domain.GroupBulletins;
import com.rcloud.server.sealtalk.domain.Groups;
import com.rcloud.server.sealtalk.domain.Users;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: xiuwei.nie
 * @Date: 2020/7/6
 * @Description:
 * @Copyright (c) 2020, rongcloud.cn All Rights Reserved
 */
@Slf4j
@Service
public class GroupsService extends AbstractBaseService<Groups, Integer> {

    @Resource
    private GroupsMapper mapper;

    @Resource
    private GroupBulletinsService groupBulletinsService;

    @Override
    protected Mapper<Groups> getMapper() {
        return mapper;
    }

    /**
     * 根据groupId更新群成员数量
     * @param groupId
     * @param memberCount
     * @param timestamp
     */
    public void updateMemberCount(Integer groupId, Integer memberCount, Long timestamp) {
        Assert.notNull(groupId,"groupId is null");
        Assert.notNull(memberCount,"memberCount is null");
        Assert.notNull(timestamp,"timestamp is null");
        Groups groups = new Groups();
        groups.setId(groupId);
        groups.setMemberCount(memberCount);
        groups.setTimestamp(timestamp);
        this.updateByPrimaryKeySelective(groups);
    }

    /**
     * 更新memberCount 和creatorId
     * @param groupId
     * @param memberCount
     * @param timestamp
     * @param creatorId
     */
    public void updateMemberCountAndCreatorId(Integer groupId, Integer memberCount, Long timestamp, Integer creatorId) {
        Assert.notNull(groupId,"groupId is null");
        Assert.notNull(memberCount,"memberCount is null");
        Assert.notNull(timestamp,"timestamp is null");
        Assert.notNull(creatorId,"creatorId is null");

        Groups groups = new Groups();
        groups.setId(groupId);
        groups.setMemberCount(memberCount);
        groups.setTimestamp(timestamp);
        groups.setCreatorId(creatorId);
        this.updateByPrimaryKeySelective(groups);
    }
    /**
     * 查询群组列表信息 (不含讨论组)
     */
    public List<Groups> queryGroups(Integer currentUserId) {
        return  this.mapper.queryGroups(currentUserId);
    }

    /**
     * 查询推荐群组列表信息
     */
    public List<Groups> recommendGroups() {
        List<Groups> groups = this.mapper.queryRecommendGroups();
        //获取最新的公告内容
        for (Groups group: groups) {
            Example example = new Example(GroupBulletins.class);
            example.createCriteria().andEqualTo("groupId", group.getId());
            example.setOrderByClause("createdAt DESC");
            List<GroupBulletins> byExample = groupBulletinsService.getByExample(example);
            if (byExample.size() > 0) {
                group.setNoticeContent(byExample.get(0).getContent());
            }
        }
        return groups;
    }
}
